package org.viettan.android.main.xml;

import java.io.IOException;

import org.viettan.android.main.io.UrlUtility;
import org.viettan.android.main.model.Campaign;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.google.inject.Inject;

public class CampaignXmlParser extends BaseXmlParser<Campaign> {

    @Inject
    CampaignXmlParser() {
	super("article");
    }

    @Override
    protected void parseProperty(Campaign campaign, XmlPullParser parser)
	    throws XmlPullParserException, IOException {
	String tagName = parser.getName();
	if (tagName.equalsIgnoreCase("title")) {
	    campaign.setTitle(parser.nextText());
	} else if (tagName.equalsIgnoreCase("idsec")) {
	    campaign.setId(Integer.parseInt(parser.nextText()));
	} else if (tagName.equalsIgnoreCase("media:thumbnail")) {
	    campaign.setImageSmallUrl(UrlUtility.parseUrl(parser.getAttributeValue(null, "url")));
	} else if (tagName.equalsIgnoreCase("media:thumbnail2x")) {
	    campaign.setImageLargeUrl(UrlUtility.parseUrl(parser.getAttributeValue(null, "url")));
	}
    }

    @Override
    protected Campaign createEntity(XmlPullParser parser) {
	return new Campaign();
    }
}
